/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of TranscoderPerseus.
 *
 * TranscoderPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * TranscoderPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with TranscoderPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.transcoder;
import org.apache.commons.cli.*;

/**
 * Command Line Interface to SaxTranscoder.
 *
 * @author Matteo Romanello, matteo [dot] romanello [at] gmail [dot] com
 */
public class TranscoderCli {

    /**
     * <code>main</code> method of Transcoder Command Line Interface.
     * <code>inFile</code> is the file to transcode<br/>
     * <code>outFile</code>  is the output file<br/>
     * <code>transFile</code> is the table for transcodification, typically in the form:<br/>
     * \\uXXXX+ \\t \\uYYYY+,<br/>
     * i.e. one or more unicode utf-8 chars or codes, followed by tabulation, followed by one or more utf-8 chars or codes<br/>
     * \\uxxxx (i.e. backslash followed by <code>u</code> followed by four lowercase <code>x</code>) on the right side of tabulation
     * means that the code(s) on the left side must be cancelled.<br/>
     * <code>reverseBool</code> false: left codes transformed in right codes; true: right codes transformed in left codes.<br/>
     * (<code>triggerTag</code>||<code>triggerAttr</code>=<code>Val</code>) transcode only substrings contained inside the <code>triggerTag</code>
     * or transcode only substring contained in tags with <code>triggerAttr=Val</code>
     *
     * @param args <code>inFile</code> <code>outFile</code> <code>transFile</code> <code>reverseBool</code> (<code>triggerTag</code>||<code>triggerAttr</code>=<code>Val</code>)
     */
    public static void main(String[] args) {

        SaxTranscoder saxTrans = new SaxTranscoder();

        Options options = new Options();

        Option inputfile   = OptionBuilder.withArgName("file" )
                                .hasArg()
                                .withDescription("sgml file to be converted to xml format" )
                                .create( "input" );

        Option outputfile   = OptionBuilder.withArgName("file" )
                                .hasArg()
                                .withDescription("xml file to be created" )
                                .create( "output" );

        Option transfile   = OptionBuilder.withArgName("file" )
                                .hasArg()
                                .withDescription("text file containing rules for the transcoder" )
                                .create( "transfile" );

        Option reverse   = new Option("reverse","use the transcoder file reading it from right to left");

        Option regexp   = OptionBuilder.withArgName("regexp" )
                                .hasArg()
                                .withDescription("regular expression" )
                                .create( "regexp" );

        options.addOption(inputfile);
        options.addOption(outputfile);
        options.addOption(transfile);
        options.addOption(reverse);
        options.addOption(regexp);

        CommandLineParser clp = new GnuParser();
        HelpFormatter formatter = new HelpFormatter();
        try {
            // parse the command line arguments
            CommandLine cl = clp.parse( options, args );

            if( cl.hasOption( "input" )&& cl.hasOption( "output" ) && cl.hasOption( "transfile" )&& cl.hasOption( "regexp" )) {
                Boolean rev = cl.hasOption( "reverse" );
                saxTrans.parse(cl.getOptionValue("input"), cl.getOptionValue("output"), cl.getOptionValue("transfile"),rev,cl.getOptionValue("regexp"));
            }
            else{
                formatter.printHelp( "Transcoder Command Line Interface", options );
            }
        }
        catch( ParseException exp ) {
            // oops, something went wrong
            System.err.println( "Parsing failed.  Reason: " + exp.getMessage() );
            formatter.printHelp( "Transcoder Command Line Interface", options );
        }
    }
}
